<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<base href="${pageContext.request.contextPath}/">
	<link type="text/css" href="bootstrap/css/bootstrap.min.css" rel="stylesheet">
	<link type="text/css" href="bootstrap-table/bootstrap-table.min.css" rel="stylesheet">
	<link type="text/css" href="bootstrap-datetimepicker/css/bootstrap-datetimepicker.min.css" rel="stylesheet">
</head>
<body>

	<!-- 关联市场活动的模态窗口 -->
	<div class="modal fade" id="bundModal" role="dialog">
		<div class="modal-dialog" role="document" style="width: 80%;">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal">
						<span aria-hidden="true">×</span>
					</button>
					<h4 class="modal-title">关联市场活动</h4>
				</div>
				<div class="modal-body">
					<div class="btn-group" style="position: relative; top: 18%; left: 8px;">
						<form class="form-inline" role="form">
						  <div class="form-group has-feedback">
						    <input type="text" class="form-control" style="width: 300px;" placeholder="请输入市场活动名称，支持模糊查询" id="name">

						  </div>
							<div class="form-group has-feedback">
								<button type="button" class="btn btn-default" onclick="$('#clueTable').bootstrapTable('refresh');">搜索&emsp;<span class="glyphicon glyphicon-search form-control-feedback"></span></button>
							</div>
						</form>
					</div>
				</div>
				<div class="modal-body">
					<table id="clueTable" class="table table-hover"></table>
				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
					<button type="button" class="btn btn-primary" data-dismiss="modal" onclick="saveRelation()">关联</button>
				</div>
			</div>
		</div>
	</div>
	<!-- 修改线索备注的模态窗口 -->
	<div class="modal fade" id="editRemarkModal" role="dialog">
		<div class="modal-dialog" role="document" style="width: 40%;">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal">
						<span aria-hidden="true">×</span>
					</button>
					<h4 class="modal-title" id="myModalLabel">修改备注</h4>
				</div>
				<div class="modal-body">
					<form class="form-horizontal" role="form">
						<%-- 备注的id --%>
						<input type="hidden" id="edit-id">
						<div class="form-group">
							<label for="edit-noteContent" class="col-sm-2 control-label">内容</label>
							<div class="col-sm-10" style="width: 81%;">
								<textarea class="form-control" rows="3" id="edit-noteContent"></textarea>
							</div>
						</div>
					</form>
				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
					<button type="button" class="btn btn-primary" onclick="edit()">更新</button>
				</div>
			</div>
		</div>
	</div>

	<!-- 返回按钮 -->
	<div style="position: relative; top: 35px; left: 10px;">
		<a href="javascript:void(0);" onclick="window.history.back();"><span class="glyphicon glyphicon-arrow-left" style="font-size: 20px; color: #DDDDDD"></span></a>
	</div>
	
	<!-- 大标题 -->
	<div style="position: relative; left: 40px; top: -30px;">
		<div class="page-header">
			<h3>${clue.fullName}${clue.appellation} <small>${clue.company}</small></h3>
		</div>
		<div style="position: relative; height: 50px; width: 500px;  top: -72px; left: 700px;">
			<button type="button" class="btn btn-default" href="workbench/clue/convert"><span class="glyphicon glyphicon-retweet"></span> 转换</button>
			
		</div>
	</div>
	
	<br/>
	<br/>
	<br/>

	<!-- 详细信息 -->
	<div style="position: relative; top: -70px;">
		<div style="position: relative; left: 40px; height: 30px;">
			<div style="width: 300px; color: gray;">名称</div>
			<div style="width: 300px;position: relative; left: 200px; top: -20px;"><b>${clue.fullName}${clue.appellation}</b></div>
			<div style="width: 300px;position: relative; left: 450px; top: -40px; color: gray;">所有者</div>
			<div style="width: 300px;position: relative; left: 650px; top: -60px;"><b>${clue.userName}</b></div>
			<div style="height: 1px; width: 400px; background: #D5D5D5; position: relative; top: -60px;"></div>
			<div style="height: 1px; width: 400px; background: #D5D5D5; position: relative; top: -60px; left: 450px;"></div>
		</div>
		<div style="position: relative; left: 40px; height: 30px; top: 10px;">
			<div style="width: 300px; color: gray;">公司</div>
			<div style="width: 300px;position: relative; left: 200px; top: -20px;"><b>${clue.company}</b></div>
			<div style="width: 300px;position: relative; left: 450px; top: -40px; color: gray;">职位</div>
			<div style="width: 300px;position: relative; left: 650px; top: -60px;"><b>${clue.job}</b></div>
			<div style="height: 1px; width: 400px; background: #D5D5D5; position: relative; top: -60px;"></div>
			<div style="height: 1px; width: 400px; background: #D5D5D5; position: relative; top: -60px; left: 450px;"></div>
		</div>
		<div style="position: relative; left: 40px; height: 30px; top: 20px;">
			<div style="width: 300px; color: gray;">邮箱</div>
			<div style="width: 300px;position: relative; left: 200px; top: -20px;"><b>${clue.email}</b></div>
			<div style="width: 300px;position: relative; left: 450px; top: -40px; color: gray;">公司座机</div>
			<div style="width: 300px;position: relative; left: 650px; top: -60px;"><b>${clue.phone}</b></div>
			<div style="height: 1px; width: 400px; background: #D5D5D5; position: relative; top: -60px;"></div>
			<div style="height: 1px; width: 400px; background: #D5D5D5; position: relative; top: -60px; left: 450px;"></div>
		</div>
		<div style="position: relative; left: 40px; height: 30px; top: 30px;">
			<div style="width: 300px; color: gray;">公司网站</div>
			<div style="width: 300px;position: relative; left: 200px; top: -20px;"><b>${clue.website}</b></div>
			<div style="width: 300px;position: relative; left: 450px; top: -40px; color: gray;">手机</div>
			<div style="width: 300px;position: relative; left: 650px; top: -60px;"><b>${clue.mphone}</b></div>
			<div style="height: 1px; width: 400px; background: #D5D5D5; position: relative; top: -60px;"></div>
			<div style="height: 1px; width: 400px; background: #D5D5D5; position: relative; top: -60px; left: 450px;"></div>
		</div>
		<div style="position: relative; left: 40px; height: 30px; top: 40px;">
			<div style="width: 300px; color: gray;">线索状态</div>
			<div style="width: 300px;position: relative; left: 200px; top: -20px;"><b>${clue.state}</b></div>
			<div style="width: 300px;position: relative; left: 450px; top: -40px; color: gray;">线索来源</div>
			<div style="width: 300px;position: relative; left: 650px; top: -60px;"><b>${clue.source}</b></div>
			<div style="height: 1px; width: 400px; background: #D5D5D5; position: relative; top: -60px;"></div>
			<div style="height: 1px; width: 400px; background: #D5D5D5; position: relative; top: -60px; left: 450px;"></div>
		</div>
		<div style="position: relative; left: 40px; height: 30px; top: 50px;">
			<div style="width: 300px; color: gray;">创建者</div>
			<div style="width: 500px;position: relative; left: 200px; top: -20px;"><b>${clue.createBy}&nbsp;&nbsp;</b><small style="font-size: 10px; color: gray;">${clue.createTime}</small></div>
			<div style="height: 1px; width: 550px; background: #D5D5D5; position: relative; top: -20px;"></div>
		</div>
		<div style="position: relative; left: 40px; height: 30px; top: 60px;">
			<div style="width: 300px; color: gray;">修改者</div>
			<div style="width: 500px;position: relative; left: 200px; top: -20px;"><b>${clue.editBy}&nbsp;&nbsp;</b><small style="font-size: 10px; color: gray;">${clue.editTime}</small></div>
			<div style="height: 1px; width: 550px; background: #D5D5D5; position: relative; top: -20px;"></div>
		</div>
		<div style="position: relative; left: 40px; height: 30px; top: 70px;">
			<div style="width: 300px; color: gray;">描述</div>
			<div style="width: 630px;position: relative; left: 200px; top: -20px;">
				<b>
					${clue.description}
				</b>
			</div>
			<div style="height: 1px; width: 850px; background: #D5D5D5; position: relative; top: -20px;"></div>
		</div>
		<div style="position: relative; left: 40px; height: 30px; top: 80px;">
			<div style="width: 300px; color: gray;">联系纪要</div>
			<div style="width: 630px;position: relative; left: 200px; top: -20px;">
				<b>
					${clue.contactSummary}
				</b>
			</div>
			<div style="height: 1px; width: 850px; background: #D5D5D5; position: relative; top: -20px;"></div>
		</div>
		<div style="position: relative; left: 40px; height: 30px; top: 90px;">
			<div style="width: 300px; color: gray;">下次联系时间</div>
			<div style="width: 300px;position: relative; left: 200px; top: -20px;"><b>${clue.nextContactTime}</b></div>
			<div style="height: 1px; width: 400px; background: #D5D5D5; position: relative; top: -20px; "></div>
		</div>
        <div style="position: relative; left: 40px; height: 30px; top: 100px;">
            <div style="width: 300px; color: gray;">详细地址</div>
            <div style="width: 630px;position: relative; left: 200px; top: -20px;">
                <b>
					${clue.address}
                </b>
            </div>
            <div style="height: 1px; width: 850px; background: #D5D5D5; position: relative; top: -20px;"></div>
        </div>
	</div>
	
	<!-- 备注 -->
	<div style="position: relative; top: 40px; left: 40px;">
		<div class="page-header">
			<h4>备注</h4>
		</div>
		<div id="remarkList"></div>
		<div id="remarkDiv" style="background-color: #E6E6E6; width: 870px; height: 90px;">
			<form id="dataForm" role="form" style="position: relative;top: 10px; left: 10px;">
            <textarea id="save-noteContent" class="form-control" style="width: 850px; resize : none;" rows="2"
					  placeholder="添加备注..."></textarea>
				<p id="cancelAndSaveBtn" style="position: relative;left: 737px; top: 10px; display: none;">
					<button id="cancelBtn" type="button" class="btn btn-default">取消</button>
					<button type="button" class="btn btn-primary" onclick="save()">保存</button>
				</p>
			</form>
		</div>
	</div>
	
	<!-- 市场活动 -->
	<div>
		<div style="position: relative; top: 60px; left: 40px;">
			<div class="page-header">
				<h4>市场活动</h4>
			</div>
			<div style="position: relative;top: 0px; width: 870px;">
				<table id="clueTable-on" class="table table-hover"></table>
			</div>
			<br>
			<div>
				<a href="javascript:void(0);" data-toggle="modal" data-target="#bundModal" style="text-decoration: none;"><span class="glyphicon glyphicon-plus"></span>关联市场活动</a>
			</div>
		</div>
	</div>
	
	
	<div style="height: 200px;"></div>
	<script type="text/javascript" src="js/jquery-3.6.0.js"></script>
	<script type="text/javascript" src="bootstrap/js/bootstrap.min.js"></script>
	<script type="text/javascript" src="bootstrap-table/bootstrap-table.min.js"></script>
	<script type="text/javascript" src="bootstrap-table/locale/bootstrap-table-zh-CN.min.js "></script>
	<script type="text/javascript" src="bootstrap-datetimepicker/js/bootstrap-datetimepicker.min.js"></script>
	<script type="text/javascript" src="bootstrap-datetimepicker/js/locales/bootstrap-datetimepicker.zh-CN.js"></script>
	<script type="text/javascript">

		//默认情况下取消和保存按钮是隐藏的
		var cancelAndSaveBtnDefault = true;

		$(function(){
			initTableOn();
			initTable();

			document.onkeydown = function (e) {
				var ev = document.all ? window.event:e;
				if(ev.keyCode == 13){
					save();
					if($('#editRemarkModal').css('display')=="block"){
						edit();
					}
				}
			}
			
			$("#save-noteContent").focus(function(){
				if(cancelAndSaveBtnDefault){
					//设置remarkDiv的高度为130px
					$("#remarkDiv").css("height","130px");
					//显示
					$("#cancelAndSaveBtn").show("2000");
					cancelAndSaveBtnDefault = false;
				}
			});

			$("#cancelBtn").click(function(){
				//显示
				$("#cancelAndSaveBtn").hide();
				//设置remarkDiv的高度为130px
				$("#remarkDiv").css("height","90px");
				cancelAndSaveBtnDefault = true;
			});
			initRemarkList();
		});
		// 获取域对象中的数据
		var clueId = '${clue.id}';
		var userName = '${clue.userName}';
        // 加载备注列表
        function initRemarkList() {
            $.ajax('clueRemark/list', {
                type: 'get',
                data: {
                    clueId: clueId
                },
                success: function (res) {
                    if (res.code == 0) {
                        var str = '';
                        $.each(res.data, function () {
                            str += '<div class="remarkDiv" style="height: 60px;">';
                            str += '<img title="'+ userName +'" src="image/user-thumbnail02.png" style="width: 30px; height:30px;">';
                            str += '<div style="position: relative; top: -40px; left: 40px;">';
                            str += '<h5>'+ this.noteContent +'&emsp;&emsp;----来自 (' + this.createBy + ')</h5>';
                            str += '<small style="color: gray;">线索 - <strong style="font-size: 15px; color: black">${clue.fullName}${clue.appellation} - ${clue.company} </strong>' + this.createTime + ' 由 <span style="color: #206ae1">' + this.createBy + '</span> 添加 </small>';
                            str += '<div style="position: relative; left: 500px; top: -30px; height: 30px; width: 100px; display: none;">';
                            str += '<a class="myHref" href="javascript:openEdit(\'' + this.id + '\');"><span class="glyphicon glyphicon-edit" style="font-size: 20px; color: #E6E6E6;"></span></a>';
                            str += '&nbsp;&nbsp;&nbsp;&nbsp;';
                            str += '<a class="myHref" href="javascript:remove(\'' + this.id + '\');"><span class="glyphicon glyphicon-remove" style="font-size: 20px; color: #E6E6E6;"></span></a>';
                            str += '</div></div></div>';
                        });
                        $('#remarkList').html(str)

						$(".remarkDiv").mouseover(function(){
							$(this).children("div").children("div").show();
						});

						$(".remarkDiv").mouseout(function(){
							$(this).children("div").children("div").hide();
						});

						$(".myHref").mouseover(function(){
							$(this).children("span").css("color","red");
						});

						$(".myHref").mouseout(function(){
							$(this).children("span").css("color","#E6E6E6");
						});
                    }
                }
            })
        }

		function save(){
			$.ajax('clueRemark/save', {
				type: 'post',
				contentType: 'application/json',
				data: JSON.stringify({
					noteContent: $('#save-noteContent').val(),
					clueId: clueId,
				}),
				beforeSend: function () {
					var noteContent = $('#save-noteContent').val();
					if (!noteContent) {
						return false;
					}
				},
				success: function (res) {
					if(res.code == 0){
						cancel();
						initRemarkList();
					}
				}
			})
		}

		function remove(id) {
			if(confirm("确定删除数据吗？")){
				$.ajax('clueRemark/delete/' + id, {
					type: 'delete',
					success: function (res) {
						if(res.code == 0){
							initRemarkList();
						}
					}
				});
			}
		}

		function openEdit(id) {
			$.ajax('clueRemark/find/' + id, {
				type: 'get',
				success: function (res) {
					if(res.code == 0){
						$('#edit-id').val(res.data.id);
						$('#edit-noteContent').val(res.data.noteContent);
						$('#editRemarkModal').modal('show');
					}
				}
			})
		}

		// 修改
		function edit() {
			$.ajax('clueRemark/edit', {
				type: 'put',
				contentType: 'application/json',
				data: JSON.stringify({
					id: $('#edit-id').val(),
					noteContent: $('#edit-noteContent').val(),
				}),
				beforeSend: function () {
					var noteContent = $('#edit-noteContent').val();
					if (!noteContent) {
						alert("内容为空");
						return false;
					}
				},
				success: function (res) {
					if (res.code == 0) {
						$('#editRemarkModal').modal('hide');
						initRemarkList();
					} else {
						alert(res.message);
					}
				}
			})
		}

		function cancel(){
			//显示
			$("#cancelAndSaveBtn").hide();
			//设置remarkDiv的高度为130px
			$("#remarkDiv").css("height", "90px");
			cancelAndSaveBtnDefault = true;
			$('#dataForm')[0].reset();
		}

		// 已关联表格初始化
		function initTableOn() {
			//调用配置异步表格的方法
			$('#clueTable-on').bootstrapTable({
				//=========== 表格配置项===============
				// http 请求方式
				method: 'get',
				// 请求路径
				url: 'activity/select/' + clueId,
				contentType: 'application/json',
				queryParamsType: '',
				queryParams: function (params) {
					// 获取自定义的请求参数
					params.name = $('.name').val();
					return params;
				},
				// 处理响应的格式
				responseHandler: function (res) {
					if (res.code == 0) {
						return {
							rows: res.data.list,
							total: res.data.total
						}
					}
				},
				// 表各种的列元素描述
				columns: [
					{
						field: 'id',
						visible:false
					},{
						//列名
						title: '名称',
						field: 'name',
						halign: 'center',
					},{
						//列名
						title: '开始日期',
						field: 'startDate',
						halign: 'center'
					},{
						//列名
						title: '结束日期',
						field: 'endDate',
						halign: 'center'
					},{
						//列名
						title: '所有者',
						field: 'userName',
						halign: 'center'
					},{
						formatter: function (value, row) {
							return '<button type="button" title="解除关联" class="btn btn-primary btn-xs" onclick="removeRelation(\'' + row.id +
									'\')"><span class="glyphicon glyphicon-remove">解除关联</span></button>';
						}
					}
				]
			});
		}

		// 未关联表格初始化
		function initTable() {
			//调用配置异步表格的方法
			$('#clueTable').bootstrapTable({
				//=========== 表格配置项===============
				// http 请求方式
				method: 'get',
				// 请求路径
				url: 'activity/pageList/' + clueId,
				contentType: 'application/json',
				// 点击选中
				clickToSelect: true,
				// 查询时的参数
				queryParamsType: '',
				queryParams: function (params) {
					// 获取自定义的请求参数
					params.name = $('#name').val();
					return params;
				},
				// 处理响应的格式
				responseHandler: function (res) {
					if (res.code == 0) {
						return {
							rows: res.data.list,
							total: res.data.total
						}
					}
				},
				// 表各种的列元素描述
				columns: [
					{
						//复选框
						checkbox: true
					}, {
						field: 'id',
						visible:false
					},{
						//列名
						title: '名称',
						field: 'name',
						halign: 'center',
					},{
						//列名
						title: '开始日期',
						field: 'startDate',
						halign: 'center'
					},{
						//列名
						title: '结束日期',
						field: 'endDate',
						halign: 'center'
					},{
						//列名
						title: '所有者',
						field: 'userName',
						halign: 'center'
					}
				]
			});
		}

		// 新增关联
		function saveRelation(){
			//获取用户在表格中选择
			var rows = $('#clueTable').bootstrapTable('getSelections');
			if (rows.length == 0) {
				alert('请选择需要关联的数据.');
			} else {
				if (!confirm('确定关联该数据吗?')) {
					return;
				}
				var ids = [];
				$.each(rows, function () {
					ids.push(this.id);
				});
				$.ajax('clueActivityRelation/saveBatch', {
					type: 'post',
					data: {
                        clueId: clueId,
						ids: ids.join()
					},
					success: function (res) {
						if (res.code == 0) {
							alert('关联成功');
							$('#tab-activity').bootstrapTable('refresh');
							//刷新已关联模态框
							$('#clueTable-on').bootstrapTable('refresh');
							//刷新未关联表格模态框
							$('#clueTable').bootstrapTable('refresh');

						}else{
							alert(res.message);
						}
					}
				})
			}
		}

		//解除关联
		function removeRelation(id) {
			if (confirm('确定解除关联吗？')) {
				console.log(id);
				$.ajax('clueActivityRelation/delete/' + id + '/' + clueId, {
					type: 'delete',
					success: function (res) {
						if (res.code == 0) {
							$('#clueTable-on').bootstrapTable('refresh');
							//刷新未关联表格模态框
							$('#clueTable').bootstrapTable('refresh');
						}else{
							alert(res.message);
						}
					}
				});
			}
		}
	</script>
</body>
</html>